package cn.sciento.farm.devicemanager.common.util.qiniu;

import cn.sciento.farm.devicemanager.common.util.PropertiesFileUtil;
import com.qiniu.common.Zone;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @author wumu
 * @version V1.0
 * @date 5/10/18
 * @time 9:49 PM
 */
public class QiniuClientUtil {


    private static Logger logger = LoggerFactory.getLogger(QiniuClientUtil.class);

    //设置好账号的ACCESS_KEY和SECRET_KEY
    private static String accessKey = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_ACCESS);
    private static String secretKey = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_SECRET);
    private static String bucketName = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_BUCKET);
    private static String serverUrl = PropertiesFileUtil.getInstance(QiniuConstant.CONFIG_PATH).get(QiniuConstant.SERVER_URL);


    private static Zone z = Zone.autoZone();
    private static Configuration c = new Configuration(z);

    //创建上传对象
    private static UploadManager uploadManager = new UploadManager(c);

    public static String getToken() {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName);
        return upToken;
    }

    public static String getToken(String bucketName) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName);
        return upToken;
    }

    public static String getOverWriteToken(String key, String bucketName) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName, key);
        return upToken;
    }

    public static String getOverWriteToken(String key) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName, key);
        return upToken;
    }

    public static String getToken(String bucketName, Long expires) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName, null, expires, null);
        return upToken;
    }

    public static String getToken(Long expires) {
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucketName, null, expires, null);
        return upToken;
    }

    public static String getUrl(String key) throws UnsupportedEncodingException {
        Auth auth = Auth.create(accessKey, secretKey);
        key = URLEncoder.encode(key, "utf-8");
        String finalUrl = String.format("%s/%s", serverUrl, key);
        return finalUrl;
    }

    public static String getUrl(String key, String imgProcess) throws UnsupportedEncodingException {
        Auth auth = Auth.create(accessKey, secretKey);
        key = URLEncoder.encode(key, "utf-8");
        String finalUrl = String.format("%s/%s?%s", serverUrl, key, imgProcess);
        return finalUrl;
    }

    public static String getPrivateUrl(String publicUrl, long expireInSeconds) {
        Auth auth = Auth.create(accessKey, secretKey);
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        return finalUrl;
    }

    public static String getPrivateUrl(String publicUrl) {
        Auth auth = Auth.create(accessKey, secretKey);
        String finalUrl = auth.privateDownloadUrl(publicUrl, 3600);
        return finalUrl;
    }

    public static String getPrivateUrl(String serverUrl, String key) throws UnsupportedEncodingException {
        String encodedFileName = URLEncoder.encode(key, "utf-8");
        String publicUrl = String.format("%s/%s", serverUrl, encodedFileName);
        Auth auth = Auth.create(accessKey, secretKey);
        long expireInSeconds = 3600;//1小时，可以自定义链接过期时间
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        return finalUrl;
    }

    public static String getDefaultPrivateUrl(String key) throws UnsupportedEncodingException {
        String encodedFileName = URLEncoder.encode(key, "utf-8");
        String publicUrl = String.format("%s/%s", serverUrl, encodedFileName);
        Auth auth = Auth.create(accessKey, secretKey);
        long expireInSeconds = 3600;//1小时，可以自定义链接过期时间
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        return finalUrl;
    }

    public static String getDefaultPrivateUrl(String imgProcess, String key) throws UnsupportedEncodingException {
        String encodedFileName = URLEncoder.encode(key, "utf-8");
        String publicUrl = String.format("%s/%s?%s", serverUrl, encodedFileName, imgProcess);
        Auth auth = Auth.create(accessKey, secretKey);
        long expireInSeconds = 3600;//1小时，可以自定义链接过期时间
        String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
        return finalUrl;
    }


    public static String getKey(String url) throws MalformedURLException {
        URL urL = new URL(url);
        String path = urL.getPath();
        if (path.length() < 2) {
            return "";
        }
        return path.substring(1);
    }




}
